package com.example.seh.home.secondhand;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.example.seh.R;
import com.example.seh.util.ImgUtil;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

public class GoodsGridAdapter extends BaseAdapter {
    private Context mContext;
    private List<GoodsDTO> goods;
    private int item;
    private Bitmap bitmap;
//    private Handler handler = new Handler(Looper.myLooper()){
//        @Override
//        public void handleMessage(@NonNull Message msg) {
//            super.handleMessage(msg);
//            switch (msg.what) {
//                case 1:
//                    bitmap = (Bitmap) msg.obj;
//                    break;
//                case 2:
//                    ViewHolder viewHolder = (ViewHolder) msg.obj;
//                    viewHolder.img.setImageBitmap(bitmap);
//            }
//        }
//    };

    public GoodsGridAdapter(Context mContext, List<GoodsDTO> goods, int item) {
        this.mContext = mContext;
        this.goods = goods;
        this.item = item;
    }

    @Override
    public int getCount() {
        if (goods != null) {
            return goods.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (goods != null) {
            return goods.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (null == convertView) {
            convertView = LayoutInflater.from(mContext).inflate(item, null);
            viewHolder = new ViewHolder();
            viewHolder.img = convertView.findViewById(R.id.goods_img);
            viewHolder.name = convertView.findViewById(R.id.goods_name);
            viewHolder.detail = convertView.findViewById(R.id.goods_detail);
            viewHolder.price = convertView.findViewById(R.id.goods_price);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        if (goods.get(position).getGoodsPic() != null) {
            List<String> imgs = Arrays.asList(goods.get(position).getGoodsPic().split(";"));

//            new Thread(new Runnable() {
//                @Override
//                public void run() {
//                    try {
//                        Bitmap bitmap = ImgUtil.stringToBitmap(imgs.get(0));
//                        Message msg1 = new Message();
//                        msg1.what = 1;
//                        msg1.obj = bitmap;
//                        handler.sendMessage(msg1);
//
//                        Message msg2 = new Message();
//                        msg2.what = 2;
//                        msg2.obj = viewHolder;
//                        handler.sendMessage(msg2);
//
//
//                    } catch (IOException e) {
//                        e.printStackTrace();
//                    }
//                }
//            }).start();
            Glide.with(mContext).load(imgs.get(0)).into(viewHolder.img);
            if (!imgs.get(0).isEmpty()) {
                viewHolder.img.setVisibility(View.VISIBLE);
                Glide.with(mContext)
                        .load(imgs.get(0))
                        .diskCacheStrategy(DiskCacheStrategy.NONE)
                        .dontAnimate()
                        .listener(new RequestListener<Drawable>() {
                            @Override
                            public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                                return false;
                            }

                            @Override
                            public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                                if (target instanceof ImageView) {
                                    ((ImageView) target).setTag(imgs.get(0));  // 绑定加载请求与视图
                                }
                                return false;
                            }
                        })
                        .into(viewHolder.img);
            } else {
                Glide.with(mContext)
                        .clear(viewHolder.img);  // 清除之前的加载请求，避免复用时显示错误的图片
                viewHolder.img.setImageDrawable(null);  // 或者设置默认占位图像
            }
        } else {
            viewHolder.img.setImageResource(R.drawable.wtp);
        }

        viewHolder.name.setText(goods.get(position).getGoodsName());
        viewHolder.detail.setText(goods.get(position).getGoodsDesc());
        viewHolder.price.setText(goods.get(position).getGoodsPrice() + "");
        return convertView;
    }
    class ViewHolder{
        private ImageView img;
        private TextView name, detail, price;
    }
}